<?php
/*
  $Id: product_reviews_write.php,v 1.55 2003/06/20 14:25:58 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }

  $product_info_query = tep_db_query("select p.products_id, p.products_model, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and p.products_status = '1' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
  if (!tep_db_num_rows($product_info_query)) {
    tep_redirect(tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params(array('action'))));
  } else {
    $product_info = tep_db_fetch_array($product_info_query);
  }

  $customer_query = tep_db_query("select customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
  $customer = tep_db_fetch_array($customer_query);

  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
    $rating = tep_db_prepare_input($HTTP_POST_VARS['rating']);
    $review = tep_db_prepare_input($HTTP_POST_VARS['review']);

    $error = false;
    if (strlen($review) < REVIEW_TEXT_MIN_LENGTH) {
      $error = true;

      $messageStack->add('review', JS_REVIEW_TEXT);
    }

    if (($rating < 1) || ($rating > 5)) {
      $error = true;

      $messageStack->add('review', JS_REVIEW_RATING);
    }

    if ($error == false) {
      tep_db_query("insert into " . TABLE_REVIEWS . " (products_id, customers_id, customers_name, reviews_rating, date_added) values ('" . (int)$HTTP_GET_VARS['products_id'] . "', '" . (int)$customer_id . "', '" . tep_db_input($customer['customers_firstname']) . ' ' . tep_db_input($customer['customers_lastname']) . "', '" . tep_db_input($rating) . "', now())");
      $insert_id = tep_db_insert_id();

      tep_db_query("insert into " . TABLE_REVIEWS_DESCRIPTION . " (reviews_id, languages_id, reviews_text) values ('" . (int)$insert_id . "', '" . (int)$languages_id . "', '" . tep_db_input($review) . "')");

      tep_redirect(tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params(array('action'))));
    }
  }

  if ($new_price = tep_get_products_special_price($product_info['products_id'])) {

    //CGDiscountSpecials start
    //if ($product_info['products_price'] < $new_price)
    //  $product_info['products_price'] = $new_price + $new_price * 5 / 100; //add 5% to price if original price is greater than special prices, customers will be happy :)
    $products_price = '<s>' . $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</s> <span class="productSpecialPrice">' . $currencies->display_price_nodiscount($new_price, tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';
    //CGDiscountSpecials end

  } else {
    $products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
  }

  if (tep_not_null($product_info['products_model'])) {
    $products_name = $product_info['products_name'] . '<br><span class="smallText">[' . $product_info['products_model'] . ']</span>';
  } else {
    $products_name = $product_info['products_name'];
  }

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_REVIEWS_WRITE);

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params()));
  
  require(DIR_WS_INCLUDES . 'template_top.php');
?>

<script language="javascript">
function checkForm() {
  var error = 0;
  var error_message = "<?php echo JS_ERROR; ?>";

  var review = document.product_reviews_write.review.value;

  if (review.length < <?php echo REVIEW_TEXT_MIN_LENGTH; ?>) {
    error_message = error_message + "<?php echo JS_REVIEW_TEXT; ?>";
    error = 1;
  }

  if ((document.product_reviews_write.rating[0].checked) || (document.product_reviews_write.rating[1].checked) || (document.product_reviews_write.rating[2].checked) || (document.product_reviews_write.rating[3].checked) || (document.product_reviews_write.rating[4].checked)) {
  } else {
    error_message = error_message + "<?php echo JS_REVIEW_RATING; ?>";
    error = 1;
  }

  if (error == 1) {
    alert(error_message);
    return false;
  } else {
    return true;
  }
}
</script>

<!--middle area start-->

<div class="middle-area">
  <div class="innermidbox fl">
    <div class="innermidbox-top"></div>
    <div class="innermidbox-mid">
      <div class="pdt5 pdl20 pdr20 pdb5 minheight527px">
        <div class="breadcrumbs"><a href="index.php">Home</a> > <a href="<?=tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params(array('reviews_id', 'action')))?>">Review of <?php echo $products_name; ?></a> > <span>Write Review for <?php echo $products_name; ?></span></div>
        <div class="spacer10"></div>
        <div class="">
          <h1 class="pagetitle fl">Write Review for <?php echo $products_name; ?></h1>
          <a href="<?=tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params(array('reviews_id', 'action')))?>" class="backbtn fr"></a>
          <div class="clear"></div>
          <div class="hline"></div>
          <div class="spacer3"></div>
          <!--left panel start-->
          <?php include('includes/template_left.php'); ?>
          <!--left panel end-->
          <!--right panel start-->
          <div class="rightpanel mgl15 fl cms">
        	
            <?php echo tep_draw_form('product_reviews_write', tep_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, 'action=process&products_id=' . $HTTP_GET_VARS['products_id']), 'post', 'onSubmit="return checkForm();"'); ?>
            
            <h2 class="fl"><?php echo $products_name; ?> (<?php echo $products_price; ?>)</h2>
            
			<div class="spacer10"></div>
			<?php
              if ($messageStack->size('review') > 0) {
            ?>
                <strong><?php echo $messageStack->output('review'); ?>123123</strong>
                <div class="spacer10"></div>
            <?php
              }
            ?>
            <div class="fl">
            <div class="contact-left mgt15 w150px"><?php echo SUB_TITLE_FROM; ?></div>
            <div class="mgt15 fl"><?=tep_output_string_protected($customer['customers_firstname'] . ' ' . $customer['customers_lastname']); ?></div> 
            <div class="spacer5"></div>
            
            <div class="contact-left mgt15 w150px"><?php echo SUB_TITLE_REVIEW; ?></div>
            <div class="hinputbg-area fl mgt15" style="margin:0px;"> <?php echo tep_draw_textarea_field('review', 'soft', 50, 15,'','class="hinput-area"'); ?> </div>
            <div class="spacer5"></div>
            
            <div class="contact-left mgt15 w150px">&nbsp;</div>
            <div class="mgt15 fl"><?=TEXT_NO_HTML?></div> 
            <div class="spacer5"></div>
            
            <div class="contact-left mgt15 w150px"><?=SUB_TITLE_RATING?></div>
            <div class="mgt15 fl"><?=TEXT_BAD.' '.tep_draw_radio_field('rating', '1').' '.tep_draw_radio_field('rating', '2').' '. tep_draw_radio_field('rating', '3').' '.tep_draw_radio_field('rating', '4').' '.tep_draw_radio_field('rating', '5').' '.TEXT_GOOD; ?></div>
			<div class="spacer5"></div>
            
            <div class="contact-left mgt15 w150px">&nbsp;</div>
            <div class="mgt15 fl">
				<?php echo tep_submit('',"class='continue-btn fl'"); ?>
                <a href="<?=tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params(array('reviews_id', 'action')))?>" class="backbtn fr"></a>
            </div>
			<div class="spacer5"></div>
            </div>
            
            <div class="fr">
            <div class="addressinfobox pdl10 pdt10 pdr10 pdb10" style="width:100px;">
            	<a href="<?=tep_href_link(DIR_WS_IMAGES.$product_info['products_image'])?>" target="_blank">
					<?=tep_image(DIR_WS_IMAGES . $product_info['products_image'], $product_info['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, 'hspace="5" vspace="5"')?>
                </a>
			</div>
            <div class="spacer10"></div>
            <a href="<?=tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now')?>" class="listaddtocartbtn"></a>
            </div>
            
            </form>
            
            
          </div>
          <!--right panel end-->
          <div class="spacer10"></div>
        </div>
      </div>
    </div>
    <div class="innermidbox-bottom"></div>
  </div>
</div>
<!--middle area end-->
<div class="spacer25"></div>
</div>


<?php
require(DIR_WS_INCLUDES . 'template_bottom.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>